package cn.zyx.controller;


import cn.zyx.dao.PowerDao;
import cn.zyx.entity.Power;
import cn.zyx.service.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Power)表控制层
 *
 * @author makejava
 * @since 2021-12-19 13:29:28
 */
@RestController
@RequestMapping("power")
public class PowerController {
    /**
     * 服务对象
     */
    @Resource
    private PowerService powerService;

    @Resource
    private PowerDao powerDao;

    @Autowired
    private ReturnPayApi returnPayApi;



    /**
     * 根据openid 查询
     */
    @RequestMapping("selopenid")
    public List<Power> selopenid(String openid){
        return powerService.queryopenId(openid);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selnumber")
    public List<Power> selnumber(String number){
        return powerService.querynumber(number);
    }

    /**
     * 电费添加
     * @param power
     * @return
     */
    @RequestMapping("add")
    public Power add(@RequestBody Power power) {
        PowerController powerController = new PowerController();
        String time = powerController.time();
//        添加下单时间
        power.setStart_date(time);
//        添加充值状态
        power.setDstatus("1");
        powerService.insert(power);
        return power;
    }

    /**
     * 地区名&充值金额下拉框&充值状态
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit,String dphone,String powername, String dmoney,String dstatus,String start_date,String end_date) {
        List<Power> powers = this.powerService.queryAllByLimit((page - 1) * limit, limit, dphone, powername,  dmoney, dstatus, start_date, end_date);
        int count = powerDao.count(dphone, powername, dmoney, dstatus, start_date, end_date);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("data", powers);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(Integer id) {
        Power power = new Power();
        power.setId(id);
        power.setDstatus("2");
        PowerController powerController = new PowerController();
        String time = powerController.time();
        power.setEnd_date(time);
        powerService.update(power);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将未充值用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(Integer id, String orderNum, String money, String desc, HttpServletResponse response) {
        Power power1 = powerService.queryById(id);
//        int a = (int) (Float.valueOf(power1.getFmoney())*100);
//        returnPayApi.refund(power1.getNumber(),"",String.valueOf(a), response);
//        Map<String, Object> refund = returnPayApi.refund(power1.getNumber(), "", "1", response);
        Power power = new Power();
        power.setId(id);
        power.setDstatus("3");
        PowerController powerController = new PowerController();
        String time = powerController.time();
        power.setEnd_date(time);
        power.setNumber(orderNum);
        power.setDesc(desc);
        power.setFmoney(money);
        powerService.update(power);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(Integer id) {
        Power power = new Power();
        power.setId(id);
        power.setIsdel(1);
        PowerController powerController = new PowerController();
        String time = powerController.time();
        power.setEnd_date(time);
        powerService.update(power);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Power power = new Power();
            String s = split[i];
            int i1 = Integer.parseInt(s);
            System.out.println(s + "--");
            power.setId(i1);
            power.setIsdel(1);
            powerService.update(power);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Power selectOne(Integer id) {
        return this.powerService.queryById(id);
    }

}